package com.jmeter.perf.controller;

import java.util.List;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jmeter.perf.entity.Bank;
import com.jmeter.perf.mapper.BankMapper;
import com.jmeter.perf.service.IBankService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/bank")
@CacheConfig(cacheNames = "bank")
public class BankController {

	@Autowired
	private BankMapper mapper;

	@Autowired
	private IBankService service;

	@PostMapping
	public int insert(@RequestBody Bank bank) {
		return mapper.insert(bank);
	}

	@GetMapping("/all")
	public List<Bank> selectList() {
		return mapper.selectList(null);
	}

	@GetMapping("/{id}")
	public Bank selectList(@PathVariable("id") String id) {
		return mapper.selectById(id);
	}

	@GetMapping("/withcache/{id}")
	public Bank selectById(@PathVariable("id") String id) {
		return service.getById(id);
	}

	@GetMapping("/withcache")
	@Cacheable(key = "'pageNo,pageSize'")
	public IPage<Bank> page(@RequestParam(name = "pageNo", required = true) int pageNo, @RequestParam(name = "pageSize", required = true) int pageSize) {
		IPage<Bank> page = new Page<Bank>(pageNo, pageSize);
		return mapper.selectPage(page, null);

	}

	@GetMapping()
	public IPage<Bank> pageNoCache(@RequestParam(name = "pageNo", required = true) int pageNo, @RequestParam(name = "pageSize", required = true) int pageSize) {
		IPage<Bank> page = new Page<Bank>(pageNo, pageSize);
		return mapper.selectPage(page, null);

	}
}
